package com.offcn.order.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.order.pojo.TOrder;
import com.offcn.order.service.OrderService;
import com.offcn.order.vo.req.OrderInfoSubmitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/01/28/17:08
 * @Description: 保存订单
 */
@Api(tags = "保存订单")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/createOrder")
    @ApiOperation("保存订单")
    public AppResponse<TOrder> createOrder(@RequestBody OrderInfoSubmitVo vo) {
        String memerId = stringRedisTemplate.opsForValue().get(vo.getAccessToken());
        if (memerId == null) {
            AppResponse<TOrder> fail = AppResponse.fail(null);
            fail.setMsg("无此权限,请先登录");
            return fail;
        }
        try {
            TOrder tOrder = orderService.saveOrder(vo);
            AppResponse<TOrder> orderAppResponse = AppResponse.ok(tOrder);
            return orderAppResponse;
        } catch (Exception e) {
            e.printStackTrace();
            return AppResponse.fail(null);
        }

    }

}
